//package com.ruibang.glass.material.service.impl.zpk;
//
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
//import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.ruibang.glass.common.constants.CommonErrorCode;
//import com.ruibang.glass.common.constants.RedisKey;
//import com.ruibang.glass.common.domain.DictEntityInfo;
//import com.ruibang.glass.common.domain.camunda.BpmProcessInstanceCreate;
//import com.ruibang.glass.common.domain.camunda.BpmProcessInstanceStatus;
//import com.ruibang.glass.material.constant.Constants;
//import com.ruibang.glass.material.constant.MaterialStatusCode;
//import com.ruibang.glass.material.constant.MaterialWareHouseCode;
//import com.ruibang.glass.material.domain.req.ycl.ManageOutBoundReq;
//import com.ruibang.glass.material.domain.req.ycl.ManageOutInReq;
//import com.ruibang.glass.material.dto.OutAndInManagementDTO;
//import com.ruibang.glass.material.entity.MaterialALLInventoryManagement;
//import com.ruibang.glass.material.entity.MaterialManagement;
//import com.ruibang.glass.material.entity.ycl.MaterialManagementInventory;
//import com.ruibang.glass.material.entity.zpk.MaterialZPKOutboundManagement;
//import com.ruibang.glass.material.feign.CamundaApi;
//import com.ruibang.glass.material.mapper.zpk.GMaterialZPKOutboundManagementMapper;
//import com.ruibang.glass.material.service.GMaterialALLInventoryManagementService;
//import com.ruibang.glass.material.service.GMaterialManagementService;
//import com.ruibang.glass.material.service.impl.CommonService;
//import com.ruibang.glass.material.service.impl.GMaterialALLInventoryManagementServiceImpl;
//import com.ruibang.glass.material.service.zpk.GMaterialZPKOutboundManagementService;
//import com.teaming.cloud.framework2.autoconfigure.redis.RedisService ;
//import com.teaming.cloud.framework2.common.exception.ServiceException;
//import com.teaming.cloud.framework2.common.model.ResultBody;
//import com.teaming.cloud.framework2.common.tools.JsonUtils;
//import net.bytebuddy.implementation.bytecode.Throw;
//import org.apache.commons.lang.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.*;
//import java.util.stream.Collectors;
//
///**
// * <p>
// * 出库管理表 服务实现类
// * </p>
// *
// * @author mameng
// * @since 2024-04-01
// */
//@Service
//public class GMaterialZPKOutboundManagementServiceImpl extends ServiceImpl<GMaterialZPKOutboundManagementMapper, MaterialZPKOutboundManagement> implements GMaterialZPKOutboundManagementService {
//
//    @Autowired
//    private CommonService commonService;
//
//    @Autowired
//    private CamundaApi camundaApi;
//
//    @Autowired
//    private GMaterialALLInventoryManagementService materialALLInventoryManagementService;
//
//    @Autowired
//    private GMaterialManagementService materialManagementService;
//
//
//    @Autowired
//    private RedisService redisService;
//
//    @Override
//    public IPage<MaterialZPKOutboundManagement> getPage(ManageOutBoundReq materialInventoryReq) {
//        LambdaQueryWrapper<MaterialZPKOutboundManagement> queryWrapper = Wrappers.<MaterialZPKOutboundManagement>lambdaQuery();
//        if (StringUtils.isNotBlank(materialInventoryReq.getOrderNumber())) {
//            queryWrapper.like(MaterialZPKOutboundManagement::getOrderNumber, materialInventoryReq.getOrderNumber());
//        }
//        if (StringUtils.isNotBlank(materialInventoryReq.getType())) {
//            queryWrapper.like(MaterialZPKOutboundManagement::getType, materialInventoryReq.getType());
//        }
//        if (StringUtils.isNotBlank(materialInventoryReq.getWarehouseName())) {
//            queryWrapper.like(MaterialZPKOutboundManagement::getWarehouseName, materialInventoryReq.getWarehouseName());
//        }
//        if (StringUtils.isNotBlank(materialInventoryReq.getInNumber())) {
//            queryWrapper.like(MaterialZPKOutboundManagement::getOutNumber, materialInventoryReq.getInNumber());
//        }
//        if (StringUtils.isNotBlank(materialInventoryReq.getStatus())) {
//            queryWrapper.like(MaterialZPKOutboundManagement::getStatus, materialInventoryReq.getStatus());
//        }
//        if (StringUtils.isNotBlank(materialInventoryReq.getApproveStatus())) {
//            queryWrapper.like(MaterialZPKOutboundManagement::getApproveStatus, materialInventoryReq.getApproveStatus());
//        }
//        if (StringUtils.isNotBlank(materialInventoryReq.getPreparer())) {
//            queryWrapper.like(MaterialZPKOutboundManagement::getPreparer, materialInventoryReq.getPreparer());
//        }
//        if (StringUtils.isNotBlank(materialInventoryReq.getBatchNumber())) {
//            queryWrapper.like(MaterialZPKOutboundManagement::getBatchNumber, materialInventoryReq.getBatchNumber());
//        }
//
//        if (StringUtils.isNotBlank(materialInventoryReq.getWareHouseStatus())) {
//            queryWrapper.like(MaterialZPKOutboundManagement::getWarehouseStatus, materialInventoryReq.getWareHouseStatus());
//        }
//
//        if (StringUtils.isNotBlank(materialInventoryReq.getWareHouseCode())) {
//            queryWrapper.eq(MaterialZPKOutboundManagement::getWarehouseCode, materialInventoryReq.getWareHouseCode());
//        }
//
//        if (StringUtils.isNotBlank(materialInventoryReq.getItemName())) {
//            queryWrapper.like(MaterialZPKOutboundManagement::getItemName, materialInventoryReq.getItemName());
//        }
//        if (StringUtils.isNotBlank(materialInventoryReq.getBarcodeNumber())) {
//            queryWrapper.like(MaterialZPKOutboundManagement::getBarcodeNumber, materialInventoryReq.getBarcodeNumber());
//        }
//        if (materialInventoryReq.getFinishStartDate() != null) {
//            queryWrapper.ge(MaterialZPKOutboundManagement::getFinishDate, materialInventoryReq.getFinishStartDate());
//        }
//        if (materialInventoryReq.getFinishEndDate() != null) {
//            queryWrapper.le(MaterialZPKOutboundManagement::getFinishDate, materialInventoryReq.getFinishEndDate());
//        }
//        Page<MaterialZPKOutboundManagement> materialWarehouseInfoPage = baseMapper.selectPage(new Page<>(materialInventoryReq.getPageNo(), materialInventoryReq.getPageSize()),
//                queryWrapper.orderByDesc(MaterialZPKOutboundManagement::getCreationTime)
//        );
//
//        if (CollectionUtils.isEmpty(materialWarehouseInfoPage.getRecords())) {
//            return materialWarehouseInfoPage;
//        }
//        Map<Object, Object> userMap = redisService.hmget(RedisKey.USER_GROUP_TYPE);
//        List<String> businessKeys = materialWarehouseInfoPage.getRecords().stream().map(MaterialZPKOutboundManagement::getId).collect(Collectors.toList());
//        Map<String, Integer> statusMap = null;
//        Map<String, String> processMap = null;
//        Map<String, String> approveStatusMap = null;
//        Map<String, String> userIdMap = null;
//        Map<String, String> nodeName = null;
//        //获取业务流程状态
//        ResultBody<List<BpmProcessInstanceStatus>> resultBody = camundaApi.queryProcessInstanceStatus(businessKeys);
//        if (resultBody != null && CollectionUtils.isNotEmpty(resultBody.getData())) {
//            statusMap = resultBody.getData().stream().collect(Collectors.toMap(BpmProcessInstanceStatus::getBusinessKey, BpmProcessInstanceStatus::getStatus, (s1, s2) -> s1));
//            processMap = resultBody.getData().stream().collect(Collectors.toMap(BpmProcessInstanceStatus::getBusinessKey, BpmProcessInstanceStatus::getProcessInstanceId, (s1, s2) -> s1));
//            approveStatusMap = resultBody.getData().stream().collect(Collectors.toMap(BpmProcessInstanceStatus::getBusinessKey, BpmProcessInstanceStatus::getResult, (s1, s2) -> s1));
//            userIdMap = resultBody.getData()
//                    .stream()
//                    .filter(status -> status.getUserId() != null)
//                    .collect(Collectors.toMap(
//                            BpmProcessInstanceStatus::getBusinessKey,
//                            BpmProcessInstanceStatus::getUserId,
//                            (s1, s2) -> s1
//                    ));
//            nodeName = resultBody.getData().stream().collect(Collectors.toMap(BpmProcessInstanceStatus::getBusinessKey, BpmProcessInstanceStatus::getNodeName, (s1, s2) -> s1));
//
//        }
//        for (MaterialZPKOutboundManagement qualityContact : materialWarehouseInfoPage.getRecords()) {
//            if (statusMap != null) {
//                qualityContact.setStatus(statusMap.get(qualityContact.getId()).toString());
//            }
//            if (processMap != null) {
//                qualityContact.setProcessId(processMap.get(qualityContact.getId()));
//            }
//            if (approveStatusMap != null) {
//                qualityContact.setApproveStatus(approveStatusMap.get(qualityContact.getId()));
//            }
//            if (nodeName != null) {
//                qualityContact.setNodeName(nodeName.get(qualityContact.getId()));
//            }
//            if (approveStatusMap != null) {
//                String userId = userIdMap.get(qualityContact.getId());
//                if (StringUtils.isNotBlank(userId)) {
//                    qualityContact.setApproveUser(userMap.get(userId).toString());
//                }
//            }
//
//        }
//        return materialWarehouseInfoPage;
//    }
//
//
//    @Override
//    public IPage<OutAndInManagementDTO> getPage(ManageOutInReq manageOutInReq) {
//        return baseMapper.getPage(new Page<>(manageOutInReq.hashCode(), manageOutInReq.getPageSize()), manageOutInReq);
//    }
//
//    public String getApproveStatus(String id) {
//        List<String> ids = new ArrayList<>();
//        ids.add(id);
//        ResultBody<List<BpmProcessInstanceStatus>> resultBody = camundaApi.queryProcessInstanceStatus(ids);
//        Map<String, String> approveStatusMap = null;
//        if (resultBody != null && CollectionUtils.isNotEmpty(resultBody.getData())) {
//            approveStatusMap = resultBody.getData().stream().collect(Collectors.toMap(BpmProcessInstanceStatus::getBusinessKey, BpmProcessInstanceStatus::getResult, (s1, s2) -> s1));
//        }
//        if (approveStatusMap.get(id) != null) {
//            return approveStatusMap.get(id);
//        }
//        return "";
//    }
//
//
//    @Override
//    public ResultBody addMaterialZPKOutboundManagement(MaterialZPKOutboundManagement materialZPKOutboundManagement) {
//        MaterialManagement byId = materialManagementService.getById(materialZPKOutboundManagement.getOrderId());
//        materialZPKOutboundManagement.setItemName(byId.getItemName());
//        materialZPKOutboundManagement.setItemCode(byId.getMaterialCode());
//        if (!compileQuality(materialZPKOutboundManagement)) {
//            throw new ServiceException(MaterialStatusCode.ADD_FAIL_OUT.getCode(), MaterialStatusCode.ADD_FAIL_OUT.getMsg());
//        }
//        materialZPKOutboundManagement.setUnit(byId.getUnit());
//        materialZPKOutboundManagement.setSpecification(byId.getSpecificationModel());
//        materialZPKOutboundManagement.setItemType(byId.getItemType());
//        materialZPKOutboundManagement.setSpStatus(Constants.ONE_STR);
//        baseMapper.insert(materialZPKOutboundManagement);
//        BpmProcessInstanceCreate instanceCreate = new BpmProcessInstanceCreate();
//        instanceCreate.setBusinessKey(materialZPKOutboundManagement.getId());
//        instanceCreate.setMenuId(materialZPKOutboundManagement.getMenuId());
//        //发起流程
//        if (StringUtils.isNotBlank(materialZPKOutboundManagement.getUsePersonId())) {
//            Map<String, Object> variables = new HashMap<>();
//            variables.put(com.ruibang.glass.common.constants.Constants.PROCESS_INSTANCE_STARTER_USER_ID, materialZPKOutboundManagement.getUsePersonId());
//            variables.put(com.ruibang.glass.common.constants.Constants.PROCESS_INSTANCE_BUSINESS_KEY, materialZPKOutboundManagement.getId());
//            variables.put(com.ruibang.glass.common.constants.Constants.PROCESS_INSTANCE_MENU_ID, materialZPKOutboundManagement.getMenuId());
//            instanceCreate.setVariables(variables);
//        }
//        commonService.startProcess(instanceCreate);
//        return ResultBody.success(MaterialStatusCode.ADD_SUCCESS_OUT.getMsg());
//    }
//
//    @Override
//    public void updateMaterialZPKOutboundManagement(MaterialZPKOutboundManagement materialZPKOutboundManagement) {
//        MaterialZPKOutboundManagement materialZPKOutboundManagement1 = baseMapper.selectById(materialZPKOutboundManagement.getId());
//        materialZPKOutboundManagement1.setRemarks(materialZPKOutboundManagement.getRemarks());
//        baseMapper.updateById(materialZPKOutboundManagement1);
//    }
//
//    //计算库存
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void updateItemHouse(MaterialZPKOutboundManagement materialZPKOutboundManagement) {
//        //检验流程是否结束
//        String approveStatus = getApproveStatus(materialZPKOutboundManagement.getId());
//        if (!StringUtils.equals(approveStatus, Constants.TWO_STR)) {
//            return;
//        }
//        MaterialZPKOutboundManagement materialZPKOutboundManagement1 = baseMapper.selectById(materialZPKOutboundManagement.getId());
//        if (StringUtils.isBlank(materialZPKOutboundManagement.getItemCode())) {
//            throw new ServiceException(MaterialStatusCode.NO_PRODUCT_CODE.getCode(), MaterialStatusCode.NO_PRODUCT_CODE.getMsg());
//        }
//        MaterialALLInventoryManagement one = materialALLInventoryManagementService.getOne(Wrappers.<MaterialALLInventoryManagement>lambdaQuery()
//                .eq(MaterialALLInventoryManagement::getMaterialName, materialZPKOutboundManagement.getItemName())
//                .eq(MaterialALLInventoryManagement::getMaterialCode, materialZPKOutboundManagement.getItemCode())
//                .last("limit 1"));
//
//        if (materialZPKOutboundManagement.getQuantity() == null) {
//            throw new ServiceException(MaterialStatusCode.WARE_HOUSE_QUALITY_LESS_USE_QUALITY.getCode(), MaterialStatusCode.WARE_HOUSE_QUALITY_LESS_USE_QUALITY.getMsg());
//        }
//        if (one == null) {
//            throw new ServiceException(MaterialStatusCode.NO_PRODUCT_CODE.getCode(), MaterialStatusCode.NO_PRODUCT_CODE.getMsg());
//        }
//        Integer quality = one.getStockQuantity();
//        Integer quantity = materialZPKOutboundManagement.getQuantity();
//        if (quality < quantity) {
//            throw new ServiceException(MaterialStatusCode.NO_IN_QUALITY_NUMBER.getCode(), MaterialStatusCode.NO_IN_QUALITY_NUMBER.getMsg());
//        }
//        Integer count = quality - quantity;
//        one.setStockQuantity(count);
//        materialALLInventoryManagementService.updateById(one);
//        materialZPKOutboundManagement1.setFinishDate(new Date());
//        materialZPKOutboundManagement1.setOutStatus(Constants.TWO_STR);
//        baseMapper.updateById(materialZPKOutboundManagement1);
//    }
//
//    @Override
//    public ResultBody getDictList() {
//        List<DictEntityInfo> list = JsonUtils.parseArray((String) redisService.get(RedisKey.DICT_NEW_GROUP_TYPE), DictEntityInfo.class);
//        return ResultBody.success(list);
//    }
//
//    //校验出库申请 数量和库存数量比较
//    public Boolean compileQuality(MaterialZPKOutboundManagement materialZPKOutboundManagement) {
//        if (StringUtils.isBlank(materialZPKOutboundManagement.getItemCode())) {
//            return false;
//        }
//        MaterialALLInventoryManagement one = materialALLInventoryManagementService.getOne(Wrappers.<MaterialALLInventoryManagement>lambdaQuery()
//                .eq(MaterialALLInventoryManagement::getMaterialName, materialZPKOutboundManagement.getItemName())
//                .eq(MaterialALLInventoryManagement::getMaterialCode, materialZPKOutboundManagement.getItemCode())
//                .last("limit 1"));
//        if (one.getStockQuantity() == null) {
//            return false;
//        }
//        if (one.getStockQuantity() < materialZPKOutboundManagement.getQuantity()) {
//            return false;
//        }
//        return true;
//    }
//
//
//}
